﻿
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using T.FAS.Runtime.Base.Common;
using T.FAS.Runtime.Base.Dao;
using T.FAS.Runtime.Base.Entity;

namespace T.FAS.Runtime.Base.Service
{
    public class QueryPlanService
    {
        readonly QueryPlanDao dao = new QueryPlanDao();
        readonly QueryPlanContentDao contentDao = new QueryPlanContentDao();
        readonly QueryPlanDetailDao detailDao = new QueryPlanDetailDao();
        readonly QueryPlanUserDao userDao = new QueryPlanUserDao();
        readonly QueryPlanDefaultDao defaultDao = new QueryPlanDefaultDao();
        readonly ChartInstanceDao instanceDao = new ChartInstanceDao();
        readonly ChartInstanceApplyDao applyDao = new ChartInstanceApplyDao();
        #region 1.0版本方案，弃用
        /// <summary>
        /// 获取查询方案列表
        /// </summary>
        /// <returns></returns>
        public List<QueryPlan> GetQueryPlanList(string pageID, string DQID)
        {
            string userID = UserUtil.GetCurrentUserID();
            List<QueryPlan> queryPlans = dao.GetQueryPlanList(pageID, DQID, userID);
            string modifyUsers = ConfigUtil.Instance.ModifyAuthUser ?? "";
            bool hasModifyAuth = modifyUsers.Contains(userID);
            foreach (var item in queryPlans)
            {
                item.IsAuth = hasModifyAuth;
            }
            return queryPlans;
        }
        /// <summary>
        /// 创建或更新
        /// </summary>
        /// <param name = "queryPlan"></param>
        public Dictionary<string, string> CreateOrUpdateQueryPlan(QueryPlan queryPlan)
        {
            var dic = new Dictionary<string, string>();
            string modifyUsers = ConfigUtil.Instance.ModifyAuthUser ?? "";
            string userName = UserUtil.GetCurrentUserName();
            queryPlan.UserName = userName;
            queryPlan.LastModifyTime = DateTime.Now;
            queryPlan.LastModifier = userName;
            queryPlan.UserID = UserUtil.GetCurrentUserID();
            if (!modifyUsers.Contains(queryPlan.UserID) && queryPlan.PlanRange == PlanRange.Public)
            {
                dic.Add("state", "0");
                dic.Add("errmsg", $"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T_FAS_fasr_Base_1620238293", "您没有权限修改公共方案！")}");
                return dic;
            }
            try
            {
                var content = new QueryPlanContent { ColumnContent = queryPlan.ColumnContent, CrossColumn = queryPlan.CrossColumn, FilterContent = queryPlan.FilterContent, LastModifier = queryPlan.LastModifier, LastModifyTime = queryPlan.LastModifyTime, SortContent = queryPlan.SortContent, UseColumnContent = queryPlan.UseColumnContent, };
                var listViewSet = new ListViewSet { pageSize = "10", showAutoFilterRow = false, displayColumns = new List<object>(), sortColumns = new List<object>(), };
                if (!string.IsNullOrEmpty(queryPlan.ColumnContent))
                    listViewSet.displayColumns = JsonConvert.DeserializeObject<List<object>>(queryPlan.ColumnContent);
                if (!string.IsNullOrEmpty(queryPlan.SortContent))
                    listViewSet.sortColumns = JsonConvert.DeserializeObject<List<object>>(queryPlan.SortContent);
                var detail = new QueryPlanDetail { FilterContent = queryPlan.FilterContent, FilterShow = queryPlan.FilterContent, ListViewSet = JsonConvert.SerializeObject(listViewSet), CardViewSet = queryPlan.CardViewSet, PivotViewSet = queryPlan.PivotViewSet, TreeViewSet = queryPlan.TreeViewSet, LastModifier = queryPlan.LastModifier, LastModifyTime = queryPlan.LastModifyTime, };
                using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))
                {
                    if (string.IsNullOrEmpty(queryPlan.ID))
                    {
                        queryPlan.ID = Guid.NewGuid().ToString();
                        queryPlan.IsSystem = false;
                        queryPlan.PlanOrder = dao.GetMaxOrder(queryPlan);
                        if (queryPlan.IsDefault)
                            dao.UpdateDefault(queryPlan);
                        content.ID = Guid.NewGuid().ToString();
                        contentDao.Add(new List<object> { content });
                        detail.ID = content.ID;
                        detailDao.Add(new List<object> { detail });
                        queryPlan.ContentID = content.ID;
                        dao.Add(new List<object> { queryPlan });
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(queryPlan.ContentID))
                            throw new Exception($"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T_FAS_fasr_Base_58138230", "修改查询方案ContentID不能为空！")}");
                        if (queryPlan.IsDefault)
                            dao.UpdateDefault(queryPlan);
                        content.ID = queryPlan.ContentID;
                        contentDao.Update(new List<object> { content });
                        detail.ID = queryPlan.ContentID;
                        detailDao.Update(new List<object> { detail });
                        dao.Update(new List<object> { queryPlan });
                    }
                    scope.Complete();
                }
                dic.Add("state", "1");
                dic.Add("message", $"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T-Standard-Common-00000085", "保存成功")}");
                dic.Add("queryPlanID", queryPlan.ID);
            }
            catch (Exception e)
            {
                dic.Add("state", "0");
                dic.Add("errmsg", e.ToString());
            }
            return dic;
        }
        #endregion
        /// <summary>
        /// 获取查询方案列表
        /// </summary>
        /// <returns></returns>
        public List<QueryPlan> GetQueryPlanListV2(QueryPlanFilter queryPlanFilter)
        {
            string modifyUsers = ConfigUtil.Instance.ModifyAuthUser ?? "";
            queryPlanFilter.UserID = UserUtil.GetCurrentUserID();
            queryPlanFilter.HasModifyAuth = modifyUsers.Contains(queryPlanFilter.UserID);
            List<QueryPlan> queryPlans = dao.GetQueryPlanListV2(queryPlanFilter);
            return queryPlans;
        }
        /// <summary>
        /// 获取查询方案列表不带明细V3
        /// </summary>
        /// <returns></returns>
        public List<QueryPlan> GetQueryPlanListV3(QueryPlanFilter queryPlanFilter)
        {
            string modifyUsers = ConfigUtil.Instance.ModifyAuthUser ?? "";
            queryPlanFilter.UserID = UserUtil.GetCurrentUserID();
            queryPlanFilter.HasModifyAuth = modifyUsers.Contains(queryPlanFilter.UserID);
            List<QueryPlan> queryPlans = dao.GetQueryPlanListV3(queryPlanFilter);
            return queryPlans;
        }
        /// <summary>
        /// 根据ID查询方案
        /// </summary>
        /// <returns></returns>
        public QueryPlan GetQueryPlanV2(string planId)
        {
            QueryPlan queryPlan = dao.GetObject(planId);
            if (queryPlan != null)
            {
                string userID = UserUtil.GetCurrentUserID();
                string modifyUsers = ConfigUtil.Instance.ModifyAuthUser ?? "";
                queryPlan.IsDefault = defaultDao.GetQueryPlanDefault(planId);
                if (string.IsNullOrEmpty(queryPlan.SharedBy))
                {
                    queryPlan.SharedBy = queryPlan.UserName;
                }
                dao.GetQueryPlanDetail(queryPlan);
                dao.HandlePlanInfo(queryPlan, userID, modifyUsers.Contains(userID), true);
            }
            return queryPlan;
        }
        /// <summary>
        /// 创建或更新
        /// </summary>
        /// <param name = "queryPlan"></param>
        public Dictionary<string, string> CreateOrUpdateQueryPlanV2(QueryPlan queryPlan)
        {
            var dic = new Dictionary<string, string>();
            string modifyUsers = ConfigUtil.Instance.ModifyAuthUser ?? "";
            string userName = UserUtil.GetCurrentUserName();
            queryPlan.UserName = userName;
            queryPlan.LastModifyTime = DateTime.Now;
            queryPlan.LastModifier = userName;
            queryPlan.UserID = UserUtil.GetCurrentUserID();
            if (!modifyUsers.Contains(queryPlan.UserID) && queryPlan.PlanRange == PlanRange.Public)
            {
                dic.Add("state", "0");
                dic.Add("errmsg", $"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T_FAS_fasr_Base_1620238293", "您没有权限修改公共方案！")}");
                return dic;
            }
            try
            {
                string orginalContentID = "";
                var content = new QueryPlanDetail
                {
                    FilterContent = queryPlan.FilterContent,
                    FilterShow = queryPlan.FilterShow,
                    ListViewSet = queryPlan.ListViewSet,
                    ChartViewSet = queryPlan.ChartViewSet,
                    CardViewSet = queryPlan.CardViewSet,
                    PivotViewSet = queryPlan.PivotViewSet,
                    TreeViewSet = queryPlan.TreeViewSet,
                    LastModifier = queryPlan.LastModifier,
                    LastModifyTime = queryPlan.LastModifyTime,
                    WarningID = queryPlan.WarningID,
                    //门户方案明细
                    PortalFilterContent = queryPlan.PortalFilterContent,
                    //不能删掉，编辑时用
                    PortalId = queryPlan.PortalId,
                    DefaultPortalId = queryPlan.DefaultPortalId,
                };

                using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))
                {
                    if (string.IsNullOrEmpty(queryPlan.ID))//新增
                    {
                        if (queryPlan.PlanType == PlanType.PortalPlan)
                        {  //新增时需要同时根据老的门户实例id生成新的实例
                            SPI.IQueryPlanService service = PlanServiceCache.GetInstance().GetIQueryPlanServices("Portal");
                            //创建空门户实例
                            if (string.IsNullOrEmpty(queryPlan.PortalId))
                            {
                                var id = service.CreateEmptyPlan(queryPlan);
                                if (id == null)
                                {
                                    dic.Add("state", "0");
                                    dic.Add("errmsg", "Portal Instance null");
                                    return dic;
                                }
                            }
                            else
                            {
                                service.CreateNewPlanDetail(queryPlan);
                            }
                            //门户方案明细
                            content.PortalId = queryPlan.PortalId;
                            content.DefaultPortalId = queryPlan.DefaultPortalId;
                        }
                        orginalContentID = queryPlan.ContentID;
                        queryPlan.ID = Guid.NewGuid().ToString();
                        queryPlan.PlanOrder = dao.GetMaxOrder(queryPlan);//获取排序
                        queryPlan.IsSystem = false;
                        queryPlan.WarningID = "";
                        queryPlan.SharedBy = "";
                        if (queryPlan.IsDefault)
                            defaultDao.DefaultQueryPlan(queryPlan, true);
                        content.ID = Guid.NewGuid().ToString();
                        detailDao.Add(new List<object> { content });
                        queryPlan.ContentID = content.ID;
                        queryPlan.IsDefault = false;
                        dao.Add(new List<object> { queryPlan });
                    }
                    else//修改
                    {
                        if (string.IsNullOrEmpty(queryPlan.ContentID))
                            throw new Exception($"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T_FAS_fasr_Base_58138230", "修改查询方案ContentID不能为空！")}");
                        if (queryPlan.IsDefault)
                            defaultDao.DefaultQueryPlan(queryPlan, true);
                        //门户默认方案
                        if (!string.IsNullOrEmpty(queryPlan.PortalId) && string.IsNullOrEmpty(queryPlan.DefaultPortalId) && queryPlan.IsSystem)
                        {
                            content.DefaultPortalId = queryPlan.PortalId;
                            SPI.IQueryPlanService service = PlanServiceCache.GetInstance().GetIQueryPlanServices("Portal");
                            service.CreateNewPlanDetail(queryPlan);
                            content.PortalId = queryPlan.PortalId;
                        }
                        content.ID = queryPlan.ContentID;
                        dao.UpdateQueryPlanName(queryPlan.ContentID, queryPlan.Name);
                        detailDao.Update(new List<object> { content });
                        dao.ExceptColumnName.Add("IsDefault");
                        dao.Update(new List<object> { queryPlan });
                        dao.ExceptColumnName.Remove("IsDefault");
                    }

                    if (queryPlan.Charts?.Count > 0)//存在图表信息
                    {
                        applyDao.Delete(new List<string> { queryPlan.ContentID });
                        var chartIds = applyDao.GetChartInstanceApplyIDs(TargetType.QueryPlan, queryPlan.ID);
                        instanceDao.Delete(chartIds);
                        var chartInstances = new List<ChartInstance>();
                        int order = 0;
                        foreach (var chart in queryPlan.Charts)
                        {
                            chart.ID = Guid.NewGuid().ToString();
                            chart.LastModifyTime = DateTime.Now;
                            chart.LastModifier = userName;
                            chart.TargetType = TargetType.QueryPlan;
                            chart.TargetID = queryPlan.ContentID;
                            chart.SortOrder = order++;
                            var instance = new ChartInstance { ID = Guid.NewGuid().ToString(), LastModifier = chart.LastModifier, LastModifyTime = chart.LastModifyTime, Code = chart.Code, Name = chart.Name, ApplyID = chart.ID, ChartLibItemID = chart.ChartLibItemID, DataSourceID = chart.DataSourceID, DemoData = chart.DemoData, ChartContent = chart.ChartContent, };
                            chart.ChartInstanceID = instance.ID;
                            chartInstances.Add(instance);
                        }
                        instanceDao.Add(chartInstances);
                        applyDao.Add(queryPlan.Charts);
                    }
                    else if (!string.IsNullOrEmpty(orginalContentID))//另存时没有图表信息获取一下存入
                    {
                        var instanceApplies = applyDao.GetChartInstanceApplys(TargetType.QueryPlan, orginalContentID);
                        var chartInstances = new List<ChartInstance>();
                        int order = 0;
                        foreach (var chart in instanceApplies)
                        {
                            chart.ID = Guid.NewGuid().ToString();
                            chart.LastModifyTime = DateTime.Now;
                            chart.LastModifier = userName;
                            chart.TargetType = TargetType.QueryPlan;
                            chart.TargetID = queryPlan.ContentID;
                            chart.SortOrder = order++;
                            var instance = new ChartInstance { ID = Guid.NewGuid().ToString(), LastModifier = chart.LastModifier, LastModifyTime = chart.LastModifyTime, Code = chart.Code, Name = chart.Name, ApplyID = chart.ID, ChartLibItemID = chart.ChartLibItemID, DataSourceID = chart.DataSourceID, DemoData = chart.DemoData, ChartContent = chart.ChartContent, };
                            chart.ChartInstanceID = instance.ID;
                            chartInstances.Add(instance);
                        }
                        instanceDao.Add(chartInstances);
                        applyDao.Add(instanceApplies);
                    }
                    scope.Complete();
                }
                dic.Add("state", "1");
                dic.Add("message", $"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T-Standard-Common-00000085", "保存成功")}");
                dic.Add("queryPlanID", queryPlan.ID);
            }
            catch (Exception e)
            {
                dic.Add("state", "0");
                dic.Add("errmsg", e.ToString());
            }
            return dic;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name = "id"></param>
        public Dictionary<string, string> DeleteQueryPlan(string id)
        {
            var dic = new Dictionary<string, string>();
            QueryPlan queryPlan = dao.GetObject(id);

            string userID = UserUtil.GetCurrentUserID();
            if (queryPlan != null)
            {
                string modifyUsers = ConfigUtil.Instance.ModifyAuthUser ?? "";
                if (!modifyUsers.Contains(userID) && queryPlan.PlanRange == PlanRange.Public)
                {
                    dic.Add("state", "0");
                    dic.Add("errmsg", $"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T_FAS_fasr_Base_1126480642", "您没有权限删除公共方案！")}");
                    return dic;
                }
                if (queryPlan.IsSystem)
                {
                    dic.Add("state", "0");
                    dic.Add("message", $"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T_FAS_fasr_Base_1498413981", "系统方案不可删除")}");
                    return dic;
                }
                try
                {
                    using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))
                    {
                        dao.DeletePlan(queryPlan.ContentID);//方案
                        contentDao.Delete(new List<string>() { queryPlan.ContentID }); //老版查询方案内容
                        detailDao.Delete(new List<string>() { queryPlan.ContentID }); //新版查询方案内容
                        userDao.DeletePlan(id); //置顶方案信息
                        defaultDao.DeletePlan(id);//默认方案信息
                        applyDao.Delete(new List<string> { queryPlan.ContentID }); //图表信息
                        var chartIds = applyDao.GetChartInstanceApplyIDs(TargetType.QueryPlan, id);//获取图表实例
                        instanceDao.Delete(chartIds); //图表实例信息
                        scope.Complete();
                    }
                    dic.Add("state", "1");
                    dic.Add("message", $"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T_FAS_fasr_Base_882904134", "删除成功")}");
                }
                catch (Exception e)
                {
                    dic.Add("state", "0");
                    dic.Add("errmsg", e.ToString());
                }
            }

            return dic;
        }
        /// <summary>
        /// 更新查询方案
        /// </summary>
        /// <param name = "id"></param>
        public Dictionary<string, string> UpdateQueryPlan(string planID, Dictionary<string, object> param)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            try
            {
                string userID = UserUtil.GetCurrentUserID();
                QueryPlan queryPlan = dao.GetObject(planID);
                if (queryPlan != null)
                {
                    if (param.ContainsKey("Default"))//更新默认
                    {
                        bool isDefault = Convert.ToBoolean(param["Default"]);
                        defaultDao.DefaultQueryPlan(queryPlan, isDefault);
                    }
                    if (param.ContainsKey("PlanName"))//更新方案名称
                    {
                        string planName = Convert.ToString(param["PlanName"]);
                        if (!string.IsNullOrEmpty(planName))
                        {
                            queryPlan.Name = planName;
                            dao.UpdateQueryPlanName(queryPlan.ContentID, planName);
                        }
                    }
                    if (param.ContainsKey("WarningID"))//更新预警ID
                    {
                        string warningID = Convert.ToString(param["WarningID"]);
                        detailDao.UpdateWaringID(queryPlan.ContentID, warningID);
                    }
                }
                dic.Add("state", "1");
                dic.Add("message", $"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T-Standard-Common-00000740", "修改成功")}");
            }
            catch (Exception e)
            {
                dic.Add("state", "0");
                dic.Add("errmsg", e.ToString());
            }
            return dic;
        }
        /// <summary>
        /// 保存查询方案顺序
        /// </summary>
        /// <param name = "id"></param>
        public Dictionary<string, string> SaveQueryPlans(List<QueryPlan> queryPlans)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            try
            {
                dao.UpdateOrder(queryPlans);
                dic.Add("state", "1");
                dic.Add("message", $"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T-Standard-Common-00000085", "保存成功")}");
            }
            catch (Exception e)
            {
                dic.Add("state", "0");
                dic.Add("errmsg", e.ToString());
            }
            return dic;
        }
        /// <summary>
        /// 置顶
        /// </summary>
        /// <param name = "planID"></param>
        /// <param name = "topType">0置顶 1取消置顶</param>
        /// <returns></returns>
        public Dictionary<string, string> TopQueryPlan(string planID, int topType)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            try
            {
                string userID = UserUtil.GetCurrentUserID();
                userDao.DeletePlan(planID, userID);
                if (topType == 0)
                    userDao.Add(new List<object> { new QueryPlanUser { ID = Guid.NewGuid().ToString(), PlanID = planID, UserID = userID, TopTime = DateTime.Now } });
                dic.Add("state", "1");
                dic.Add("message", $"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T_FAS_fasr_Base_287344771", "设置成功")}");
            }
            catch (Exception e)
            {
                dic.Add("state", "0");
                dic.Add("errmsg", e.ToString());
            }
            return dic;
        }
        /// <summary>
        /// 分享
        /// </summary>
        public Dictionary<string, string> ShareQueryPlan(QueryPlan queryPlan, List<QueryPlan> shareUsers)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            try
            {
                string userID = UserUtil.GetCurrentUserID();
                string UserName = UserUtil.GetCurrentUserName();
                List<QueryPlan> queryPlans = new List<QueryPlan>();
                foreach (var item in shareUsers)
                {
                    if (item.UserID == userID)//如果是自己跳过
                        continue;
                    item.ID = Guid.NewGuid().ToString();
                    item.ContentID = queryPlan.ContentID;
                    item.DQID = queryPlan.DQID;
                    item.PageID = queryPlan.PageID;
                    item.BusinessTag = queryPlan.BusinessTag;
                    item.Name = queryPlan.Name;
                    item.PlanOrder = 1000;
                    item.PlanRange = PlanRange.Shared;
                    item.SharedBy = UserName;
                    item.PlanType = queryPlan.PlanType;
                    item.DefaultView = queryPlan.DefaultView;
                    item.LastModifier = UserName;
                    item.LastModifyTime = DateTime.Now;
                    QueryPlan queryPlan1 = dao.GetQueryPlan(queryPlan.ContentID, item.UserID);//是否已有方案
                    if (queryPlan1 != null)
                        queryPlans.Add(queryPlan1);
                    else
                        queryPlans.Add(item);
                }
                using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))//共享方案，先删后加
                {
                    dao.DeleteSharePlan(queryPlan.ContentID, queryPlan.UserID);
                    if (queryPlans.Count > 0)
                    {
                        dao.Add(queryPlans);
                        queryPlan.PlanRange = PlanRange.Share;
                    }
                    else
                    {
                        queryPlan.PlanRange = PlanRange.Private;
                    }
                    dao.Update(new List<object> { queryPlan });
                    scope.Complete();
                }
                dic.Add("state", "1");
                dic.Add("message", $"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T_FAS_fasr_Base_299144670", "分享成功")}");
            }
            catch (Exception e)
            {
                dic.Add("state", "0");
                dic.Add("errmsg", e.ToString());
            }
            return dic;
        }


        /// <summary>
        /// 加入分享
        /// </summary>
        public Dictionary<string, string> JoinShareQueryPlan(QueryPlan queryPlan, List<QueryPlan> shareUsers)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            try
            {
                string userID = UserUtil.GetCurrentUserID();
                if (queryPlan.UserID == userID)
                {//如果是自己跳过
                    dic.Add("message", "");
                    dic.Add("state", "1");
                    return dic;
                }
                string UserName = UserUtil.GetCurrentUserName();
                List<QueryPlan> queryPlans = new List<QueryPlan>();
                foreach (var item in shareUsers)
                {
                    item.ID = Guid.NewGuid().ToString();
                    // item.UserID = userID;
                    item.ContentID = queryPlan.ContentID;
                    item.DQID = queryPlan.DQID;
                    item.PageID = queryPlan.PageID;
                    item.BusinessTag = queryPlan.BusinessTag;
                    item.Name = queryPlan.Name;
                    item.PlanOrder = 1000;
                    item.PlanRange = PlanRange.Shared;
                    item.SharedBy = queryPlan.UserName;
                    item.PlanType = queryPlan.PlanType;
                    item.DefaultView = queryPlan.DefaultView;
                    item.LastModifier = UserName;
                    item.LastModifyTime = DateTime.Now;
                    QueryPlan queryPlan1 = dao.GetQueryPlan(queryPlan.ContentID, item.UserID);//是否已有方案
                    if (queryPlan1 == null)
                        queryPlans.Add(item);
                    // break;
                }
                if(queryPlans.Count == 0) {
                    dic.Add("state", "1");
                    dic.Add("message", "");
                }
                else
                {
                    using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))//共享方案
                    {
                        // dao.DeleteSharePlan(queryPlan.ContentID, queryPlan.UserID);                        
                        dao.Add(queryPlans);
                        queryPlan.PlanRange = PlanRange.Share;
                        dao.Update(new List<object> { queryPlan });
                        scope.Complete();
                    }
                    dic.Add("message", $"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T_FAS_fasr_Base_299144670", "分享成功")}");
                    dic.Add("state", "1");
                }
                
            }
            catch (Exception e)
            {
                dic.Add("state", "0");
                dic.Add("errmsg", e.ToString());
            }
            return dic;
        }
    }
}